<template>
  <view>
    <!-- 商品列表区域 -->
      <view class="goods-list">
          <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
           <my-goods :goods="goods"></my-goods>
          </view>
          <view class="text-prompt">
            <text class="final-data" v-show="showFinalData">数据加载完毕...</text>
          </view>
        </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数对象
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:0,
        showFinalData:false,
      };
    },
    onLoad(options){
       // console.log(options);
       // 防止出错''
      this.queryObj.query=options.query||''
      this.queryObj.cid=options.cid||''
      this.getGoodsList()
    },
    methods:{
     async getGoodsList(cb){
       const {data:res}=await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
       // 只要数据请求完毕，就立即按需调用 cb 回调函数
         cb && cb()
         // 短路运算符,存在调用,不存在不调用
         
       if(res.meta.status!==200)return uni.$showMsg()
       console.log(res);
       // this.goodsList=res.message.goods
        // 为数据赋值：通过展开运算符的形式，进行新旧数据的拼接
         this.goodsList = [...this.goodsList,...res.message.goods]
       this.total=res.message.total
      },
      // 点击跳转到商品详情页面
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    //触底事件
    onReachBottom(){
       // 让页码值自增 +1
       if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total)return this.showFinalData="ture"
      this.queryObj.pagenum+=1
      this.getGoodsList()
    },
    onPullDownRefresh(){
      this.queryObj.pagenum=1
      this.total=0
      this.goodsList=[]
      
        // 2. 重新发起请求
        this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
.text-prompt{
  line-height: 100rpx;
  text-align: center;
  .final-data{
    font-size: 12px;
    color: #aaa;
  }
}
</style>
